using System;
using System.Drawing;
using System.Collections.Generic;

namespace CSharp_Tetris
{
    public class GameSquare
    {
        public Rectangle rect;
        public SolidBrush brush;
    }
	/// <summary>
	/// Layout of the game area. Divided into 10x10 blocks within a rows x cols array
	/// </summary>
    /// 
	public class GameGrid
	{
		//private Rectangle[][] arrGameGrid;
		//private SolidBrush[][] arrGameGridBrushes;
        public GameSquare[][] squares;
		private SolidBrush[] arrBrushColours;

		public GameGrid(int intGameGridRows, int intGameGridColumns)
		{
            squares = new GameSquare[intGameGridRows][];
			//arrGameGridBrushes	= new SolidBrush[intGameGridRows][];
			arrBrushColours		= new SolidBrush[5];

			// create the GameGrid array and GameGridBrushes array
			for (int i=0;i<intGameGridRows;i++)
			{
                squares[i] = new GameSquare[intGameGridColumns];
				//arrGameGridBrushes[i]	= new SolidBrush[intGameGridColumns];
			}
			// create the BrushColours array
			arrBrushColours[0] = new SolidBrush(Color.Blue);
			arrBrushColours[1] = new SolidBrush(Color.Red);
			arrBrushColours[2] = new SolidBrush(Color.Green);
			arrBrushColours[3] = new SolidBrush(Color.Yellow);
			arrBrushColours[4] = new SolidBrush(Color.Brown);
		}

        public SolidBrush[] GetShapeColours()
		{
			return arrBrushColours;
		}
		public bool IsGridLocationEmpty(int intRowNumber, int intColNumber)
		{
			if (squares[intRowNumber][intColNumber] == null)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		public void SetShapeLocation(int intRowNumber, int intColNumber, Rectangle rctSquare, int intShapeType)
		{
            GameSquare sq = new GameSquare();
            sq.rect = rctSquare;
            sq.brush = arrBrushColours[intShapeType - 1];
            squares[intRowNumber][intColNumber] = sq;
        }

        public void DropRowsDown(int intRowNumber, int intColNumber)
		{
			if (!IsGridLocationEmpty(intRowNumber-1, intColNumber))
			{
                GameSquare sq = new GameSquare();
                sq.rect = new Rectangle(squares[intRowNumber - 1][intColNumber].rect.X,
                    squares[intRowNumber - 1][intColNumber].rect.Y + 10, 10, 10);
                sq.brush = squares[intRowNumber - 1][intColNumber].brush;
                squares[intRowNumber][intColNumber] = sq;
			}
			else
			{
                squares[intRowNumber][intColNumber] = null;
			}
		}
		public void SetTopRow()
		{
            squares[0] = new GameSquare[squares[1].Length];
		}

        public void Draw(Graphics g, int i, int k)
        {
            if (!IsGridLocationEmpty(i, k))
            {
                Rectangle r = squares[i][k].rect;
                g.FillRectangle(squares[i][k].brush, r);
                g.DrawRectangle(new Pen(Color.White, 1), r);
            }
        }

        public bool IsRowFull(int p)
        {
            // check to see if the shape causes an entire row to fill
            // if it does then we need to eliminate the row and drop the rest down
            foreach (GameSquare square in squares[p])
            {
                if (square == null) return false;
            }
            return true;
        }
    }
}

